<?php
/**
 * Created by PhpStorm.
 * User: spring
 * Date: 18-11-29
 * Time: 下午2:12
 */

namespace App\Http\Controllers\Adminer;

use App\Exceptions\BaseException;
use App\Http\Controllers\AdminController;
use App\Models\AdminLog;
use Illuminate\Http\Request;

class AdminLogController extends AdminController
{
    protected $data;
    function __construct()
    {
        parent::__construct();
        $this->data['title'] = $this->base['controller'].$this->base['action'];
        $this->data['title_en'] = "SystemLog";
        $this->data['title_url'] = route("adminlog.index");
        $this->data['is_search'] = TRUE;
    }

    public function index(Request $request){
        $where = [];
        if(!empty($request->get('keyword'))){
            $keyword = $request->get('keyword');
            $where[] = array("remark","like",'%'.$keyword.'%');
            //$this->data['keyword'] = $keyword;
            session()->put('keyword',$keyword);
        }
        $logs = AdminLog::where($where)->orderBy('id','desc')->Paginate(15);
        //dd($logs);
        $this->data['list'] = $logs;
        return view("adminer.adminlog.index",$this->data);
    }

    public function show($id)
    {
        $info = AdminLog::find($id);
        if(!$info){
            throw new BaseException('数据不存在');
        }else{
            return $this->successResponse('',$info->query,200,'');
        }
    }

    public static function writeLog($url,$query,$remark="",$user_id=""){
        $data['user_id'] = $user_id?:auth()->user()->id;
        $data['url'] = $url;
        $data['query'] = $query;
        $data['remark'] = $remark;
        $admin_log = new AdminLog($data);
        $admin_log->save();
    }

}